package com.crystal.jcms.shpz;


import com.crystal.constant.Constant;
import com.crystal.jcms.frament.BaseFragment;
import com.crystal.jcms.ui.BottomPanelLayout;
import com.crystal.jcms.ui.BottomPanelLayout.BottomPanelCallBackProtocal;
import com.crystal.jcms.ui.HeaderPanelLayout;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Window;

public class ActivityFrame extends Activity implements BottomPanelCallBackProtocal {
	private Context mContext;

	private BottomPanelLayout mBottomPanelLayout;
	private HeaderPanelLayout mHeaderPanelLayout;

	public FragmentManager mFragmentManager;
	private FragmentTransaction mFragmentTransaction = null;
	public static String currFagTag = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_frame2);
		initUI();
		mFragmentManager = getFragmentManager();
		defaultFirstScreen();
	}

	private void defaultFirstScreen() {
		mBottomPanelLayout.defaultChecked();
		setTabSection(Constant.STR_FRAGMENT_APPS);
	}

	private void initUI() {
		mBottomPanelLayout = (BottomPanelLayout) findViewById(R.id.bottom_panel);
		mBottomPanelLayout.initBottomPanel();
		mBottomPanelLayout.setCallBackProtocal(this);
		mHeaderPanelLayout = (HeaderPanelLayout) findViewById(R.id.header_panel);
		mHeaderPanelLayout.initHeaderPanel();
	}

	@Override
	public void onClickCallBack(int itemID) {
		String tag = "";
		if ((itemID & Constant.SIGN_FRAGMENT_APPS) != 0) {
			tag = Constant.STR_FRAGMENT_APPS;
		} else if ((itemID & Constant.SIGN_FRAGMENT_SET) != 0) {
			tag = Constant.STR_FRAGMENT_SET;
		}
		mHeaderPanelLayout.setText(tag);
		setTabSection(tag);
	}

	public void setTabSection(String tag) {
		if (TextUtils.equals(tag, currFagTag)) {
			return;
		}
		ensureTransaction();
		if (currFagTag != null && !currFagTag.equals("")) {
			detachFragment(getFragment(currFagTag));
		}
		attachFragment(R.id.fragment_panel, getFragment(tag), tag);
		commitTransaction();
	}

	private void detachFragment(Fragment f) {
		if (f != null && !f.isDetached()) {
			mFragmentTransaction.detach(f);
		}
	}

	private void attachFragment(int layoutId, Fragment f, String tag) {
		if (f != null) {
			if (f.isDetached()) {
				mFragmentTransaction.attach(f);
			} else if (!f.isAdded()) {
				mFragmentTransaction.add(layoutId, f, tag);
			}
		}
	}

	public Fragment getFragment(String tag) {
		BaseFragment fragment = (BaseFragment) mFragmentManager.findFragmentByTag(tag);
		if (fragment == null) {
			fragment = BaseFragment.newInstance(tag);
		}
		return fragment;
	}

	/**
	 * 创建transaction
	 */
	private void ensureTransaction() {
		if (mFragmentTransaction == null) {
			mFragmentTransaction = mFragmentManager.beginTransaction();
			mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		}
	}

	/**
	 * 提交section
	 */
	private void commitTransaction() {
		if (mFragmentTransaction != null) {
			mFragmentTransaction.commit();
			mFragmentTransaction = null;
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		currFagTag = "";
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			showTips();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void showTips() {
		AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("提示").setMessage("确定要退出吗？")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent(Intent.ACTION_MAIN);
						intent.addCategory(Intent.CATEGORY_HOME);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(intent);
						android.os.Process.killProcess(android.os.Process.myPid());
					}
				}).setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).create();
		alertDialog.show();
	}
	
	

}
